﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BHXH_NT.Properties;

namespace BHXH_NT
{
    public partial class FrmLogin : Form
    {
        public Boolean flagExit;
        public Boolean flagUser;
        private string connectionString = global::BHXH_NT.Properties.Settings.Default.MISBHXH_NTConnectionString;
        public FrmLogin()
        {
            InitializeComponent();
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            string queryString;
            if (ModuleBHXH.TestConnect() == false) { return; }
            connectionString = global::BHXH_NT.Properties.Settings.Default.MISBHXH_NTConnectionString;
            //check User Id is SA
            if (txtMaNV.Text.ToUpper() == global::BHXH_NT.Properties.Settings.Default.User.ToUpper()
                && txtMauKhau.Text.ToUpper() == global::BHXH_NT.Properties.Settings.Default.Pass.ToUpper())
            {
                flagUser = true;
                flagExit = true;
                ModuleBHXH.strUser = txtMaNV.Text;
                this.Close();
                return;
            }
            else
            {
                flagUser = false;
            }
            //check user and pass
            using (SqlConnection connection =
                  new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand command = connection.CreateCommand();
                queryString = "SELECT * FROM NT_ACCOUNT "
                   + "WHERE USERID = '" + txtMaNV.Text + "' AND PASSWORD = '" + txtMauKhau.Text + "'";
                command.CommandText = queryString;
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows == false)
                {
                    MessageBox.Show("KiÓm tra l¹i m· nh©n viªn vµ mËt khÈu");
                    txtMaNV.Focus();
                    return;
                }
            }
            //Check quyền nhân viên 
            using (SqlConnection connection =
                  new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand command = connection.CreateCommand();
                queryString = "SELECT User_Name(memberuid) As Name FROM Sysmembers "
                + "WHERE User_name(memberuid) = '" + txtMaNV.Text + "' and "
                + " User_Name(Groupuid) in ('QUANTRI','CAPSO','SUDUNG','PCPST') ";
                
                command.CommandText = queryString;
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows == true)
                {
                    flagExit = true;
                    ModuleBHXH.strUser = txtMaNV.Text;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Nh©n viªn kh«ng cã quyÒn");
                    txtMaNV.Focus();
                    return;
                }

            }
            using (SqlConnection connection =
                   new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand command = connection.CreateCommand();
                queryString = "exec sp_executesql N'SELECT User_Name(memberuid) As Name FROM Sysmembers "
                + "WHERE User_name(memberuid) = @P1 ', N'@P1 varchar(50)', '" + txtMaNV.Text + "'";
                command.CommandText = queryString;
                 SqlDataReader reader = command.ExecuteReader();
                 if (reader.HasRows == true)
                 {
                     flagExit = true;
                     ModuleBHXH.strUser = txtMaNV.Text;
                     this.Close();
                 }
                 else
                 {
                     MessageBox.Show("NhËp l¹i m· nh©n viªn");
                     txtMaNV.Focus();
                 }
               
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            flagExit = false;
            this.Close();
        }

        private void FrmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F12 && e.Shift == true)
            {
                Cursor.Current = Cursors.WaitCursor;
                frmSetting childForm = new frmSetting();
                childForm.Show();
                Cursor.Current = Cursors.Default;
             }
        }
    }
}
